#!/usr/bin/env python
# encoding: utf-8
"""
BlogEntry.py

Created by Takashi Nagai on 2009-9-25.
Copyright (c) 2009 CMScom. All rights reserved.
"""

from AccessControl import ClassSecurityInfo
from zope import interface

from Products.Archetypes.atapi import *
from Products.CMFCore.utils import getToolByName

from Products.ATContentTypes.content.document import ATDocument
from Products.ATContentTypes.content.schemata import finalizeATCTSchema
from Products.ATContentTypes.configuration import zconf

from c2.site.icos09 import icos09MessageFactory as _
from c2.site.icos09.content.interfaces import IBlogEntry
from c2.site.icos09.config import *

schema = Schema((

    StringField(
        required=False,
        name='category',
        widget=SelectionWidget(
            label='Category',
            label_msgid='icos09_label_blogentry_category',
            description_msgid = 'icos09_help_blogentry_category',
            i18n_domain='icos09',
        ),
        vocabulary='getBlogCategories',
    ),
    
    
    DateTimeField('pub_date',
            required=True,
            widget=CalendarWidget(label='pub_date',
                            description='',
                            label_msgid = "pub_date",
                            description_msgid = 'pub_date_help',
                            i18n_domain = "icos09",
                            format="%Y年%m月%d日",
                            show_hm=False,
                            ),
            ),
            
    StringField('editor',
                required=True,
                searchable=True,
                widget = StringWidget(
                        description = "",
                        description_msgid = "icos09_help_blogentry_editor",
                        label = "Editor",
                        label_msgid = "icos09_label_blogentry_editor",
                        i18n_domain = "icos09")),

    ImageField(
        required=False,
        searchable=False,
        name='picture_1',
        widget=ImageField._properties['widget'](
            label='Picture 1',
            description='',
            label_msgid='icos09_label_blogentry_picture_1',
            description_msgid = 'icos09_help_blogentry_picture_1',
            i18n_domain='icos09',
        ),
        storage=AttributeStorage(),
        swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
        pil_quality = zconf.pil_config.quality,
        pil_resize_algo = zconf.pil_config.resize_algo,
        max_size = (480,480),
        sizes= {'large'   : (768, 768),
                'preview' : (480, 480),
                'mini'    : (150, 150),
                'thumb'   : (100, 100),
                'tile'    :  (64, 64),
                'icon'    :  (32, 32),
                'listing' :  (16, 16),
                },
    ),
    
    StringField(
            name='picture_caption_1',
            required=False,
            searchable=False,
            widget=StringWidget(label='Picture Caption 1',
                            description='',
                            label_msgid = "icos09_label_blogentry_picture_caption_1",
                            description_msgid = 'icos09_help_blogentry_picture_caption_1',
                            i18n_domain = "icos09",
                            size=40
                            ),
            ),

    ImageField(
        required=False,
        searchable=False,
        name='picture_2',
        widget=ImageField._properties['widget'](
            label='Picture 2',
            description='',
            label_msgid='icos09_label_blogentry_picture_2',
            description_msgid = 'icos09_help_blogentry_picture_2',
            i18n_domain='icos09',
        ),
        storage=AttributeStorage(),
        swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
        pil_quality = zconf.pil_config.quality,
        pil_resize_algo = zconf.pil_config.resize_algo,
        max_size = (480,480),
        sizes= {'large'   : (768, 768),
                'preview' : (480, 480),
                'mini'    : (150, 150),
                'thumb'   : (100, 100),
                'tile'    :  (64, 64),
                'icon'    :  (32, 32),
                'listing' :  (16, 16),
                },
    ),
    
    StringField(
            name='picture_caption_2',
            required=False,
            searchable=False,
            widget=StringWidget(label='Picture Caption 2',
                            description='',
                            label_msgid = "icos09_label_blogentry_picture_caption_2",
                            description_msgid = 'icos09_help_blogentry_picture_caption_2',
                            i18n_domain = "icos09",
                            size=40
                            ),
            ),
    ImageField(
        required=False,
        searchable=False,
        name='picture_3',
        widget=ImageField._properties['widget'](
            label='Picture 3',
            description='',
            label_msgid='icos09_label_blogentry_picture_3',
            description_msgid = 'icos09_help_blogentry_picture_3',
            i18n_domain='icos09',
        ),
        storage=AttributeStorage(),
        swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
        pil_quality = zconf.pil_config.quality,
        pil_resize_algo = zconf.pil_config.resize_algo,
        max_size = (480,480),
        sizes= {'large'   : (768, 768),
                'preview' : (480, 480),
                'mini'    : (150, 150),
                'thumb'   : (100, 100),
                'tile'    :  (64, 64),
                'icon'    :  (32, 32),
                'listing' :  (16, 16),
                },
    ),
    
    StringField(
            name='picture_caption_3',
            required=False,
            searchable=False,
            widget=StringWidget(label='Picture Caption 3',
                            description='',
                            label_msgid = "icos09_label_blogentry_picture_caption_3",
                            description_msgid = 'icos09_help_blogentry_picture_caption_3',
                            i18n_domain = "icos09",
                            size=40
                            ),
            ),
         
    ImageField(
        required=False,
        searchable=False,
        name='picture_4',
        widget=ImageField._properties['widget'](
            label='Picture 4',
            description='',
            label_msgid='icos09_label_blogentry_picture_4',
            description_msgid = 'icos09_help_blogentry_picture_4',
            i18n_domain='icos09',
        ),
        storage=AttributeStorage(),
        swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
        pil_quality = zconf.pil_config.quality,
        pil_resize_algo = zconf.pil_config.resize_algo,
        max_size = (480,480),
        sizes= {'large'   : (768, 768),
                'preview' : (480, 480),
                'mini'    : (150, 150),
                'thumb'   : (100, 100),
                'tile'    :  (64, 64),
                'icon'    :  (32, 32),
                'listing' :  (16, 16),
                },
    ),
    
    StringField(
            name='picture_caption_4',
            required=False,
            searchable=False,
            widget=StringWidget(label='Picture Caption 4',
                            description='',
                            label_msgid = "icos09_label_blogentry_picture_caption_4",
                            description_msgid = 'icos09_help_blogentry_picture_caption_4',
                            i18n_domain = "icos09",
                            size=40
                            ),
            ),
   
),)

blog_entry_schema = getattr(ATDocument, 'schema', Schema(())).copy() + schema.copy()
finalizeATCTSchema(blog_entry_schema)

blog_entry_schema['category'].widget.visible['edit'] = 'invisible'

class BlogEntry(ATDocument):
    """
    """
    security = ClassSecurityInfo()
    interface.implements(IBlogEntry)
    schema = blog_entry_schema
    
    meta_type = "BlogEntry"
    _at_rename_after_creation = True
    
    def getBlogPicSize(self):
        #import pdb;pdb.set_trace()
        img = self.getField('picture_1')
        if img:
            return img.get_size(self)
        else:
            return 0
        return 0
    
    def getBlogCategory(self):
        return self.getCategory()  

    def getBlogCountry(self):
        return self.getCountry()
    
    def getBlogCategories(self):
        urltool = getToolByName(self, 'portal_url')
        portal = urltool.getPortalObject()
        type_name = portal.getProperty('blog_seminar_types','')
        
        return DisplayList([(str(x),str(x)) for x in type_name])
    
    def getBlogEditor(self):
        return self.getEditor()


registerType(BlogEntry, PROJECTNAME)